<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Event.php 622 2011-05-03 02:34:26Z chen.hao $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Event container
 * 
 * Event is used to give a chance that, one module could do some 
 * thing when another module send out a signal. After all observers 
 * have process their logic, the original module will continue work .
 * 
 * For a example, module 'Default' will handle log in process.
 * And it will send out a signal 'login' by a Evebit_Event object.
 * It do like this after a guest login succuessfully.
 * <code>
 * $event = new Evebit_Event("login", $user);
 * $event->dispatch();
 * </code>
 * 
 * module 'Log' want to log the username and IP address while some one login.
 * Then it register a event observer in application.ini.
 * <code>
 * events.login.0.observer = Log_Model_Observer
 * events.login.0.callback = onLogin
 * </code>
 * 
 * When event 'login' fire, system will call like this:
 * <code>
 * $observer = new Log_Model_Observer();
 * $observer->onLogin($user);
 * </code>
 * 
 * @docinfo
 * 
 * @see Evebit_Event_Observer
 * 
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Event.php 622 2011-05-03 02:34:26Z chen.hao $$
 */
class Evebit_Event extends Varien_Object {
	
	/**
	 * Create a event
	 * 
	 * @param string $code
	 */
	public function __construct($code) {
		$args = func_get_args();
		array_shift($args);
		
		$this->setCode($code);
		$this->setArguments($args);
		$this->setDispatched(false);
	}
	
	/**
	 * dispatch it
	 */
	public function dispatch() {
		if ($this->getDispatched()) {
			throw new Zend_Exception("This event '{$this->getCode()}' have been dispatched");
		}
		
		$observer = Evebit_Event_Observer::getInstance();
		$observer->dispatch($this);
	}
	
	/**
	 * Dispatch an event directly
	 * 
	 * @param string $code
	 */
	public static function event($code) {
		$args = func_get_args();
		array_shift($args);
		
		$e = new self($code);
		$e->setArguments($args);
		$e->dispatch();
	}
}